    /**
     *  This class is the main class of the "World of Zuul" application. 
     *  "World of Zuul" is a very simple, text based adventure game.  Users 
     *  can walk around some scenery. That's all. It should really be extended 
     *  to make it more interesting!
     * 
     *  To play this game, create an instance of this class and call the "play"
     *  method.
     * 
     *  This main class creates and initialises all the others: it creates all
     *  rooms, creates the parser and starts the game.  It also evaluates and
     *  executes the commands that the parser returns.
     * 
     * @author  Michael Kölling and David J. Barnes
     * @version 2016.02.29
     */
    
    public class Game 
    {
        private Parser parser;
        private Room currentRoom;
        private Player player;
            
        /**
         * Create the game and initialise its internal map.
         */
        public Game() 
        {
            createRooms();
            parser = new Parser();
            player = new Player();
        }
    
        /**
         * Create all the rooms and link their exits together.
         */
        private void createRooms()
        {
            Room outside, didu, taoyuan, beiyue, jiuyi, kongji, tonggong, yecheng;
            Room xifeng , juanyuan , wulan;
          
            // create the rooms
            outside = new Room("天阙入口");
            didu = new Room("帝都");
            taoyuan = new Room("桃源郡",new Thing("鲛人", 20));
            beiyue = new Room("北越雪城",new Thing("毒药", 20));
            jiuyi = new Room("九嶷郡",new Thing("避水珠", 20));
            kongji = new Room("空寂城",new Thing("剑技秘籍", 20));
            tonggong = new Room("铜宫",new Thing("解药", 20));
            yecheng = new Room("叶城");
            xifeng = new Room("息风郡");
            juanyuan = new Room("狷之原");
            wulan = new Room("乌兰沙海");
            // theater = new Room("in a lecture theater");
            // pub = new Room("in the campus pub");
            // lab = new Room("in a computing lab", new Thing("apple", 20));
            // office = new Room("in the computing admin office");
            // lecture = new Room("lecture room");
            //canteen = new Room("in the canteen");
            //dorm = new Room("in the dorm room");
            
            outside.setExit("west", taoyuan);
            // initialise room exits
            //outside.setExits(null, theater, lab, pub, null, null);
            taoyuan.setExit("north", beiyue);
            taoyuan.setExit("west", didu);
            taoyuan.setExit("south", yecheng);
            //outside.setExit("west", didu);
            //theater.setExits(null, null, null, outside, lecture, null);
            didu.setExit("north", juanyuan);
            didu.setExit("west", tonggong);
            didu.setExit("east", taoyuan);
            didu.setExit("south", yecheng);
            //didu.setExit("up", lecture);
            // lecture.setExits(null, null, null, null, null, theater);
            jiuyi.setExit("south", juanyuan);
            jiuyi.setExit("east", wulan);
            jiuyi.setExit("west", xifeng);
            // pub.setExits(null, outside, null, null, null, null);
            beiyue.setExit("south", taoyuan);
            beiyue.setExit("north", wulan);
            beiyue.setExit("west", juanyuan);
            // lab.setExits(outside, office, null, null, null, null);
            kongji.setExit("north", xifeng);
            kongji.setExit("south", tonggong);
            kongji.setExit("east", juanyuan);
            // office.setExits(null, null, null, lab, null, null);
            tonggong.setExit("north", kongji);
            tonggong.setExit("south", yecheng);
            tonggong.setExit("east", didu);
            
            juanyuan.setExit("south", didu);
            juanyuan.setExit("north", jiuyi);
            juanyuan.setExit("west", kongji);
            juanyuan.setExit("east", beiyue);
            
            wulan.setExit("west", jiuyi);
            wulan.setExit("south", beiyue);
            
            xifeng.setExit("east", jiuyi);
            xifeng.setExit("south", kongji);
            
            /*outside.setExits(null, theater, lab, pub, null, null);
            theater.setExits(null, null, null, outside, null, null);
            pub.setExits(null, outside, null, null, null, canteen);
            lab.setExits(outside, office, null, null, dorm, null);
            office.setExits(null, null, null, lab, null, null);
            canteen.setExits(null, null, null, null, pub, null);
            dorm.setExits(null, null, null, null, null, pub);
            */
           
            currentRoom = outside;  // start game outside
        }
    
        /**
         *  Main play routine.  Loops until end of play.
         */
        public void play() 
        {            
            printWelcome();
    
            // Enter the main command loop.  Here we repeatedly read commands and
            // execute them until the game is over.
                    
            boolean finished = false;
            while (! finished) {
                Command command = parser.getCommand();
                finished = processCommand(command);
            }
            System.out.println("中州少女记得回中州搬救兵呜呜呜");
        }
    
        /**
         * Print out the opening message for the player.
         */
        private void printWelcome()
        {
            System.out.println();
            System.out.println("这里是云荒大陆。"+
                                "地之所载，六合之间，四海之内，有仙洲名云荒。");
            System.out.println("鲛人一族身陷火海，您身负使命，登陆云荒大陆，解救鲛人一族的海皇");
            System.out.println("如果您需要帮助，请输入‘帮助’");
            System.out.println();
            System.out.println("您现在 " + currentRoom.getDescription());
            //System.out.print("Exits: ");
            currentRoom.printExits();
            /*if(currentRoom.northExit != null) {
                System.out.print("north ");
            }
            if(currentRoom.eastExit != null) {
                System.out.print("east ");
            }
            if(currentRoom.southExit != null) {
                System.out.print("south ");
            }
            if(currentRoom.westExit != null) {
                System.out.print("west ");
            }
            System.out.println();*/
        }
        
        private void ifJiaoren(){
            if(currentRoom.getThing().equals("鲛人")){
                System.out.println("桃花郡看似歌舞升平，实则污浊贪腐");
                System.out.println("您现在已经解救了鲛人的海皇苏摩");
                System.out.println("鲛人们经历百年的欺压，希望重新复国");
                System.out.println("您现在需要做的是把身负重伤的海皇送到'叶城'，那里是鲛人回到海国的起始地点");
            }
        }
    
        /**
         * Given a command, process (that is: execute) the command.
         * @param command The command to be processed.
         * @return true If the command ends the game, false otherwise.
         */
        private boolean processCommand(Command command) 
        {
            boolean wantToQuit = false;
    
            if(command.isUnknown()) {
                System.out.println("我不懂您的意思");
                return false;
            }
    
            Word commandWord = command.getCommandWord();
            switch(commandWord){
                case HELP:
                    printHelp();
                    break;
                case GO:
                    goRoom(command);
                    break;
                case QUIT:
                    wantToQuit = quit(command);
                    break;
                case LOOK:
                    lookThing();
                    break;
                case PICK:
                    pickThing();
                    break;
                case CHECK:
                    checkThing();
                    break;
                case EAT:
                    eatThing(command);
                    break;
        }
        
        /*if (commandWord.equals("help")) {
            printHelp();
        }
        else if (commandWord.equals("go")) {
            goRoom(command);
        }
        else if (commandWord.equals("quit")) {
            wantToQuit = quit(command);
        }
        else if (commandWord.equals("look")) {
            lookThing();
        }
        else if (commandWord.equals("pick")) {
            pickThing();
        }
        else if (commandWord.equals("check")) {
            checkThing();
        }
        else if (commandWord.equals("eat")) {
            eatThing(command);
        }*/

        return wantToQuit;
    }

    // implementations of user commands:

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("您登陆了云荒大陆！");
        System.out.println("您需要去解救鲛人");
        System.out.println();
        System.out.println("请输入您的决定");
        //System.out.println("   go quit help eat look check pick ");
        System.out.println("  " + CommandWords.allCommandWords());
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        int a=0,b=0;
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("去哪儿？");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.goRoom(direction);
        //Room nextRoom = null;
        /*if(direction.equals("north")) {
            nextRoom = currentRoom.northExit;
        }
        if(direction.equals("east")) {
            nextRoom = currentRoom.eastExit;
        }
        if(direction.equals("south")) {
            nextRoom = currentRoom.southExit;
        }
        if(direction.equals("west")) {
            nextRoom = currentRoom.westExit;
        }
        if(direction.equals("up")) {
            nextRoom = currentRoom.upExit;
        }
        if(direction.equals("down")) {
            nextRoom = currentRoom.downExit;
        }*/

        if (nextRoom == null) {
            System.out.println("已经没有路可以走了");
        }
        else {
            currentRoom = nextRoom;
            System.out.println("你现在 " + currentRoom.getDescription());
            //System.out.print("Exits: ");
            currentRoom.printExits();
            if(currentRoom.getDescription().equals("狷之原")){
                System.out.println("鬼怪，扣除了您30能量");
                System.out.println("您的能量目前还剩余："+player.minusStrength());
                System.out.println();
            }else if (currentRoom.getDescription().equals("息风郡")){
                System.out.println("神女，因为您的热心，增加了您20能量");
                System.out.println("您的能量目前还剩余："+player.plusStrength());
                System.out.println();
            }else{
                player.step();
                System.out.println("您的能量目前还剩余："+player.getStrength());
                System.out.println();
            }
            
            ifJiaoren();
            if(currentRoom.getDescription().equals("帝都")){
                System.out.println("帝都是沧流帝国的权利中心，我是海国安排在帝都的卧底");
                System.out.println("感谢您身为异族，却愿为海国的复兴大业奔波，若我们海国重归于海，中州大陆也可重归故土");
                System.out.println("接下来我将给您提示：");
                System.out.println();
                System.out.println("请您先去往位于北方的九嶷郡，那里是龙神故乡'苍梧之渊'所在地,龙神保有海皇的法器避水珠");
                System.out.println("接下来，需要去西方空寂之山找到当代剑圣，剑圣传与你剑技");
                System.out.println("南面需要利用剑技穿越帕孟高原，到达铜宫，那里的盗宝者有着奇珍异宝，重要的是有解救海皇的解药");
                System.out.println("最终您的背包里有避水珠，剑技秘籍，解药，以及沉睡的海皇到达叶城才算是完成任务");
                System.out.println("需要注意的是如果您进入的北约雪城，海皇会被直接带走，您也会被毒药所伤，任务失败");
            }
            
            if(currentRoom.getDescription().equals("铜宫")){
                    for(String key : player.bag.keySet()){
                        if(key=="剑技秘籍"){
                            a=1;
                            System.out.println("您通过帕孟高原来到铜宫");
                            System.out.println();
                            System.out.println("您已经集齐了避水珠、剑技秘籍、解药，并带走了鲛人");
                            System.out.println("接下来请去往叶城");
                        }
                    }
                    if(a==0){
                            System.out.println("没有剑技加持，您没能走过帕孟高原");
                            System.exit(1);
                    }
            }
            
            if(currentRoom.getDescription().equals("北越雪城")){
                System.out.println("北越雪主是最大反派");
                System.out.println("你们被他的毒药毒死了");
                abort();
            }
            
            if(currentRoom.getDescription().equals("桃源郡")){
                player.pick(currentRoom.getThings());
                currentRoom.removeThing();
            }
            
            if(currentRoom.getDescription().equals("叶城")){
                    for(String key : player.bag.keySet()){
                        if(key=="鲛人"){
                            b++;
                        }else if(key=="避水珠"){
                            b++;
                        }else if(key=="剑技秘籍"){
                            b++;
                        }else if(key=="解药"){
                            b++;
                        }
                    }
                    
                    if(b<4){
                            System.out.println("海皇没能复活，中州少女归于故土");
                            System.exit(1);
                    }else{
                        
                        System.out.println("鲛人自由，重归于海");
                        System.exit(1);
                    }
            }
            
            // if(currentRoom==createRooms.taoyuan){
                
            // }
            /*if(currentRoom.northExit != null) {
                System.out.print("north ");
            }
            if(currentRoom.eastExit != null) {
                System.out.print("east ");
            }
            if(currentRoom.southExit != null) {
                System.out.print("south ");
            }
            if(currentRoom.westExit != null) {
                System.out.print("west ");
            }
            System.out.println();*/
        }
    }
    
    private void abort(){
        System.exit(1);
    }

    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("退出？");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
    
    private void lookThing(){
        System.out.println(currentRoom.getThing());
    }
    
    private void pickThing(){
        if(currentRoom.thing==null){
            String c="这里什么都没有！";
            System.out.println(c);
        }else{
        player.pick(currentRoom.getThings());
        currentRoom.removeThing();
        }
        
        /*Thing thing =currentRoom.getThing();
        if(thing ！= null){
            player.pick(thing);
            System.out.println("Player pick " + thing.getName()-> thing + ".");
        }else {
            System.out.println("Nothing to pick.");
        }*/
    }
    
    private void checkThing(){
        if (player.bag.isEmpty()){
            System.out.println("空空如也");
        }else{
            for(String key : player.bag.keySet()){
                System.out.println(key);
            }
        }
    }
    
    private void eatThing(Command command){
        if(!command.hasSecondWord()) {
            System.out.println("吃什么");
            return;
        }
        String food = command.getSecondWord();
        player.eat(food);
    }
    
    /*private void printExists(){
        if(currentRoom.northExit != null) {
                System.out.print("north ");
            }
        if(currentRoom.eastExit != null) {
                System.out.print("east ");
            }
        if(currentRoom.southExit != null) {
                System.out.print("south ");
            }
        if(currentRoom.westExit != null) {
                System.out.print("west ");
            }
        if(currentRoom.upExit != null) {
                System.out.print("up ");
            }
        if(currentRoom.downExit != null) {
                System.out.print("down ");
            }
        System.out.println();
    }*/
}
